#include "canset.h"
#include "qtimer.h"
#include "ui_canset.h"
#include "ECanVci.h"
#include "QMessageBox"
#include "QDebug"
#include <QDateTime>
#include <QDate>
#include <QFileDialog>
#include <QMessageBox>

CANset::CANset(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CANset)
{
    ui->setupUi(this);
    canreadthread=new CanReadThread;
    cansdthread=new CanSDThread;
    show_diplay=new QTimer(this);

    modelcan.setHorizontalHeaderItem(0, new QStandardItem("系统时间") );
    modelcan.setHorizontalHeaderItem(1, new QStandardItem("设备时间") );
    modelcan.setHorizontalHeaderItem(2, new QStandardItem("CANID") );
    modelcan.setHorizontalHeaderItem(3, new QStandardItem("周期") );
    modelcan.setHorizontalHeaderItem(4, new QStandardItem("数据内容") );
    modelcan.setHorizontalHeaderItem(5, new QStandardItem("计数") );
    modelcan.setHorizontalHeaderItem(6, new QStandardItem("操作位") );

    modelcan2.setHorizontalHeaderItem(0, new QStandardItem("系统时间") );
    modelcan2.setHorizontalHeaderItem(1, new QStandardItem("设备时间") );
    modelcan2.setHorizontalHeaderItem(2, new QStandardItem("CANID") );
    modelcan2.setHorizontalHeaderItem(3, new QStandardItem("周期") );
    modelcan2.setHorizontalHeaderItem(4, new QStandardItem("数据内容") );
    modelcan2.setHorizontalHeaderItem(5, new QStandardItem("计数") );
    modelcan2.setHorizontalHeaderItem(6, new QStandardItem("操作位") );

    ui->tableView_canreaddata->setModel(&modelcan);
    ui->tableView_can2readdata->setModel(&modelcan2);
    ui->pushButton_StartCAN->setEnabled(false);
    show_diplay->stop();
    show_diplay->setInterval(200);
    show_diplay->start();
    can_index=0;
    nDeviceType=4;
    init_config.Timing0=0x01;
    init_config.Timing1= 0x1C;
    can_1_init_config.Timing0=0x01;
    can_1_init_config.Timing1= 0x1C;
    nDeviceInd=0;
    ui->pushButton_datasave->setText("开启记录");
    connect(show_diplay,SIGNAL(timeout()),this,SLOT(show_updata()));
    ui->pushButton_StartCAN->setEnabled(false);
    ui->pushButton_close->setEnabled(false);
    ui->pushButton_reset->setEnabled(false);
    ui->pushButton_off->setEnabled(false);
}

CANset::~CANset()
{
    delete ui;
}

void CANset::on_pushButton_OpenDevice_clicked()
{


    nReserved=0;
    dwRel_open = OpenDevice(nDeviceType, nDeviceInd, nReserved);
    if (dwRel_open != STATUS_OK)
    { QMessageBox::information(this,"OpenDevice",QString::fromLocal8Bit("NotOk")); }
    else
    {
        ui->pushButton_StartCAN->setEnabled(true);
        ui->pushButton_close->setEnabled(true);
        ui->pushButton_reset->setEnabled(true);
        ui->pushButton_OpenDevice->setDisabled(true);
    }
}




void CANset::on_pushButton_close_clicked()
{
    dwRel_closed=CloseDevice(nDeviceType,nDeviceType);

    if (dwRel_closed != STATUS_OK)
    { QMessageBox::information(this,"ColseDevice",QString::fromLocal8Bit("NotOk")); }
    else
    {
        ui->pushButton_OpenDevice->setEnabled(true);
        ui->pushButton_close->setDisabled(true);
        ui->pushButton_StartCAN->setEnabled(true);
        ui->pushButton_reset->setDisabled(true);
        ui->pushButton_StartCAN->setDisabled(true);
    }
}




void CANset::on_pushButton_StartCAN_clicked()
{
    init_config.AccCode=0;
    init_config.AccMask =0xffffff;
    init_config.Filter = 0;   //滤波使能 0为不使能，1为使能
    init_config.Mode=0;       //模式，0表示正常模式，1表示只听模式
    can_1_init_config.AccCode=0;
    can_1_init_config.AccMask =0xffffff;
    can_1_init_config.Filter = 0;
    can_1_init_config.Mode=0;
    if(ui->checkBox_can_0->isChecked()==1)
    {
        dwRel_init = InitCAN(nDeviceType, nDeviceInd, 0,&init_config);
        if (dwRel_init == STATUS_OK)
        {
            dwRel_start=StartCAN(nDeviceType, nDeviceInd, 0);
            if(dwRel_start ==STATUS_OK)
            {

            }
            else
            {
                QMessageBox::information(this,"CAN0 Start",QString::fromLocal8Bit("NotOk"));

            }
        }
        else
        {
            QMessageBox::information(this,"CAN0 InitCan",QString::fromLocal8Bit("NotOk"));

        }
    }

    if(ui->checkBox_can_1->isChecked()==1)
    {
        can2_dwRel_start = InitCAN(nDeviceType, nDeviceInd, 1,&can_1_init_config);
        if (can2_dwRel_start == STATUS_OK)
        {
            can2_dwRel_start=StartCAN(nDeviceType, nDeviceInd, 1);
            if(can2_dwRel_start ==STATUS_OK)
            {

            }
            else
            {
                QMessageBox::information(this,"CAN1 Start",QString::fromLocal8Bit("NotOk"));

            }
        }
        else
        {
            QMessageBox::information(this,"CAN1 InitCan",QString::fromLocal8Bit("NotOk"));

        }
    }



    if(dwRel_start ==STATUS_OK or can2_dwRel_start == STATUS_OK)
    {
        canreadthread->start();
        cansdthread->start();
        ui->pushButton_StartCAN->setDisabled(true);
        ui->pushButton_off->setEnabled(true);
        ui->checkBox_can_0->setEnabled(false);
        ui->checkBox_can_1->setEnabled(false);
        ui->comboBox_Baud_rate->setEnabled(false);
        ui->comboBox_Baud_rate_1->setEnabled(false);
    }
}

void CANset::show_updata()
{
    ReadErrInfo(4,nDeviceInd,0,&CAN_0_ERR);
    ReadErrInfo(4,nDeviceInd,1,&CAN_1_ERR);
    //qDebug()<<"CAN_0_ERR.ErrCode="<<QString::number(CAN_0_ERR.ErrCode, 16).toUpper();
    //qDebug()<<"CAN_1_ERR.ErrCode="<<QString::number(CAN_1_ERR.ErrCode, 16).toUpper();
     ui->tableView_canreaddata->viewport()->update();
     ui->tableView_can2readdata->viewport()->update();
    if((CAN_0_ERR.ErrCode&0x0100)==0x0100){ui->label_CAN_0_ErrCode->setText("设备已经打开");}
    else{if(ui->label_CAN_0_ErrCode->text()=="设备已经打开"){ui->label_CAN_0_ErrCode->setText("---");}}
    if((CAN_0_ERR.ErrCode&0x0200)==0x0200){ui->label_CAN_0_ErrCode->setText("打开设备错误");}
        else{if(ui->label_CAN_0_ErrCode->text()=="打开设备错误"){ui->label_CAN_0_ErrCode->setText("---");}}
    if((CAN_0_ERR.ErrCode&0x0400)==0x0400){ui->label_CAN_0_ErrCode->setText("设备没有打开");}
            else{if(ui->label_CAN_0_ErrCode->text()=="设备没有打开"){ui->label_CAN_0_ErrCode->setText("---");}}
    if((CAN_0_ERR.ErrCode&0x0800)==0x0800){ui->label_CAN_0_ErrCode->setText("缓冲区溢出");}
            else{if(ui->label_CAN_0_ErrCode->text()=="缓冲区溢出"){ui->label_CAN_0_ErrCode->setText("---");}}
    if((CAN_0_ERR.ErrCode&0x1000)==0x1000){ui->label_CAN_0_ErrCode->setText("此设备不存在");}
            else{if(ui->label_CAN_0_ErrCode->text()=="此设备不存在"){ui->label_CAN_0_ErrCode->setText("---");}}
    if((CAN_0_ERR.ErrCode&0x2000)==0x2000){ui->label_CAN_0_ErrCode->setText("装载动态库失败");}
            else{if(ui->label_CAN_0_ErrCode->text()=="装载动态库失败"){ui->label_CAN_0_ErrCode->setText("---");}}
    if((CAN_0_ERR.ErrCode&0x4000)==0x4000){ui->label_CAN_0_ErrCode->setText("表示为执行命令失败错误");}
            else{if(ui->label_CAN_0_ErrCode->text()=="表示为执行命令失败错误"){ui->label_CAN_0_ErrCode->setText("---");}}
    if((CAN_0_ERR.ErrCode&0x8000)==0x8000){ui->label_CAN_0_ErrCode->setText("内存不足");}
            else{if(ui->label_CAN_0_ErrCode->text()=="内存不足"){ui->label_CAN_0_ErrCode->setText("---");}}
    if((CAN_0_ERR.ErrCode&0x0001)==0x0001){ui->label_CAN_0_ErrCode->setText("CAN控制器内部FIFO溢出");}
            else{if(ui->label_CAN_0_ErrCode->text()=="CAN控制器内部FIFO溢出"){ui->label_CAN_0_ErrCode->setText("---");}}
    if((CAN_0_ERR.ErrCode&0x0002)==0x0002){ui->label_CAN_0_ErrCode->setText("CAN控制器错误报警");}
            else{if(ui->label_CAN_0_ErrCode->text()=="CAN控制器错误报警"){ui->label_CAN_0_ErrCode->setText("---");}}
    if((CAN_0_ERR.ErrCode&0x0004)==0x0004)
    {
        ui->label_CAN_0_ErrCode->setText("CAN控制器消极错误");
        if((CAN_0_ERR.Passive_ErrData[0]&0x100000)==0x100000)
        {
            ui->label_CAN_0_RS_ERR->setText("接收错误");
        }
       else
        {
          ui->label_CAN_0_RS_ERR->setText("发送错误");
        }

        ui->label_CAN_0_Receive_Err_count->setText(QString::number(CAN_0_ERR.Passive_ErrData[1]));
        ui->label_CAN_0_Send_Err_count->setText(QString::number(CAN_0_ERR.Passive_ErrData[2]));
        switch(CAN_0_ERR.Passive_ErrData[0]>>6)
        {
        case 0:
            ui-> label_CAN_0_RS_Info->setText("位错");
            break;
        case 0x01:
            ui-> label_CAN_0_RS_Info->setText("格式错");

            break;
        case 0x10:
            ui-> label_CAN_0_RS_Info->setText("填充错");
            break;
        case 0x11:
            ui-> label_CAN_0_RS_Info->setText("其它错误");
            break;
        }
    }
    else{
        if(ui->label_CAN_0_ErrCode->text()=="CAN控制器消极错误")
        {
            ui->label_CAN_0_ErrCode->setText("---");
            ui->label_CAN_0_RS_ERR->setText("---");
            ui-> label_CAN_0_RS_Info->setText("---");
        }
    }

    if((CAN_0_ERR.ErrCode&0x0008)==0x0008)
    {
        ui->label_CAN_0_ErrCode->setText("CAN控制器仲裁丢失");
        ui->label_CAN_0_ArLost_Info->setText(QString::number(CAN_0_ERR.ArLost_ErrData));
    }
    else
    {
        if(ui->label_CAN_0_ErrCode->text()=="CAN控制器仲裁丢失"){ui->label_CAN_0_ErrCode->setText("---");}

    }

    if((CAN_0_ERR.ErrCode&0x0010)==0x0010){ui->label_CAN_0_ErrCode->setText("CAN控制器总线错误");}
        else{if(ui->label_CAN_0_ErrCode->text()=="CAN控制器总线错误"){ui->label_CAN_0_ErrCode->setText("---");}}
    if((CAN_0_ERR.ErrCode&0x0020)==0x0020){ui->label_CAN_0_ErrCode->setText("CAN接收寄存器满");}
         else{if(ui->label_CAN_0_ErrCode->text()=="CAN接收寄存器满"){ui->label_CAN_0_ErrCode->setText("---");}}
    if((CAN_0_ERR.ErrCode&0x0040)==0x0040){ui->label_CAN_0_ErrCode->setText("CAN接收寄存器溢出");}
        else{if(ui->label_CAN_0_ErrCode->text()=="CAN接收寄存器溢出"){ui->label_CAN_0_ErrCode->setText("---");}}
    if((CAN_0_ERR.ErrCode&0x0050)==0x0080){ui->label_CAN_0_ErrCode->setText("CAN控制器主动错误");}
        else{if(ui->label_CAN_0_ErrCode->text()=="CAN控制器主动错误"){ui->label_CAN_0_ErrCode->setText("---");}}

    if((CAN_1_ERR.ErrCode&0x0100)==0x0100){ui->label_CAN_1_ErrCode->setText("设备已经打开");}
        else{if(ui->label_CAN_1_ErrCode->text()=="设备已经打开"){ui->label_CAN_1_ErrCode->setText("---");}}
    if((CAN_1_ERR.ErrCode&0x0200)==0x0200){ui->label_CAN_1_ErrCode->setText("打开设备错误");}
        else{if(ui->label_CAN_1_ErrCode->text()=="打开设备错误"){ui->label_CAN_1_ErrCode->setText("---");}}
    if((CAN_1_ERR.ErrCode&0x0400)==0x0400){ui->label_CAN_1_ErrCode->setText("设备没有打开");}
        else{if(ui->label_CAN_1_ErrCode->text()=="设备没有打开"){ui->label_CAN_1_ErrCode->setText("---");}}
    if((CAN_1_ERR.ErrCode&0x0800)==0x0800){ui->label_CAN_1_ErrCode->setText("缓冲区溢出");}
        else{if(ui->label_CAN_1_ErrCode->text()=="缓冲区溢出"){ui->label_CAN_1_ErrCode->setText("---");}}
    if((CAN_1_ERR.ErrCode&0x1000)==0x1000){ui->label_CAN_1_ErrCode->setText("此设备不存在");}
        else{if(ui->label_CAN_1_ErrCode->text()=="此设备不存在"){ui->label_CAN_1_ErrCode->setText("---");}}
    if((CAN_1_ERR.ErrCode&0x2000)==0x2000){ui->label_CAN_1_ErrCode->setText("装载动态库失败");}
        else{if(ui->label_CAN_1_ErrCode->text()=="装载动态库失败"){ui->label_CAN_1_ErrCode->setText("---");}}
    if((CAN_1_ERR.ErrCode&0x4000)==0x4000){ui->label_CAN_1_ErrCode->setText("表示为执行命令失败错误");}
        else{if(ui->label_CAN_1_ErrCode->text()=="表示为执行命令失败错误"){ui->label_CAN_1_ErrCode->setText("---");}}
    if((CAN_1_ERR.ErrCode&0x8000)==0x8000){ui->label_CAN_1_ErrCode->setText("内存不足");}
        else{if(ui->label_CAN_1_ErrCode->text()=="内存不足"){ui->label_CAN_1_ErrCode->setText("---");}}
    if((CAN_1_ERR.ErrCode&0x0001)==0x0001){ui->label_CAN_1_ErrCode->setText("CAN控制器内部FIFO溢出");}
        else{if(ui->label_CAN_1_ErrCode->text()=="CAN控制器内部FIFO溢出"){ui->label_CAN_1_ErrCode->setText("---");}}
    if((CAN_1_ERR.ErrCode&0x0002)==0x0002){ui->label_CAN_1_ErrCode->setText("CAN控制器错误报警");}
        else{if(ui->label_CAN_1_ErrCode->text()=="CAN控制器错误报警"){ui->label_CAN_1_ErrCode->setText("---");}}

    if((CAN_1_ERR.ErrCode&0x0004)==0x0004)
    {
        ui->label_CAN_1_ErrCode->setText("CAN控制器消极错误");
        if((CAN_1_ERR.Passive_ErrData[0]&0x100000)==0x100000)
        {
            ui->label_CAN_1_RS_ERR->setText("接收错误");
        }
        else
        {
            ui->label_CAN_1_RS_ERR->setText("发送错误");
        }

        ui->label_CAN_1_Receive_Err_count->setText(QString::number(CAN_1_ERR.Passive_ErrData[1]));
        ui->label_CAN_1_Send_Err_count->setText(QString::number(CAN_1_ERR.Passive_ErrData[2]));
        switch(CAN_1_ERR.Passive_ErrData[0]>>6)
        {
        case 0:
            ui-> label_CAN_1_RS_Info->setText("位错");
            break;
        case 0x01:
            ui-> label_CAN_1_RS_Info->setText("格式错");

            break;
        case 0x10:
            ui-> label_CAN_1_RS_Info->setText("填充错");
            break;
        case 0x11:
            ui-> label_CAN_1_RS_Info->setText("其它错误");
            break;
        }
    }
    else{
        if(ui->label_CAN_1_ErrCode->text()=="CAN控制器消极错误")
        {
            ui->label_CAN_1_ErrCode->setText("---");
            ui->label_CAN_1_ErrCode->setText("---");
            ui->label_CAN_1_RS_ERR->setText("---");
            ui-> label_CAN_1_RS_Info->setText("---");
        }
    }

    if((CAN_1_ERR.ErrCode&0x0008)==0x0008)
    {
        ui->label_CAN_1_ErrCode->setText("CAN控制器仲裁丢失");
        ui->label_CAN_1_ArLost_Info->setText(QString::number(CAN_1_ERR.ArLost_ErrData));
    }
    else
    {
         if(ui->label_CAN_1_ErrCode->text()=="CAN控制器仲裁丢失")
        {
            ui->label_CAN_1_ErrCode->setText("---");
            ui->label_CAN_1_ArLost_Info->setText("---");
        }

    }
    if((CAN_1_ERR.ErrCode&0x0010)==0x0010){ui->label_CAN_1_ErrCode->setText("CAN控制器总线错误");}
        else{if(ui->label_CAN_1_ErrCode->text()=="CAN控制器总线错误"){ui->label_CAN_1_ErrCode->setText("---");}}
    if((CAN_1_ERR.ErrCode&0x0020)==0x0020){ui->label_CAN_1_ErrCode->setText("CAN接收寄存器满");}
        else{if(ui->label_CAN_1_ErrCode->text()=="CAN接收寄存器满"){ui->label_CAN_1_ErrCode->setText("---");}}
    if((CAN_1_ERR.ErrCode&0x0040)==0x0040){ui->label_CAN_1_ErrCode->setText("CAN接收寄存器溢出");}
        else{if(ui->label_CAN_1_ErrCode->text()=="CAN接收寄存器溢出"){ui->label_CAN_1_ErrCode->setText("---");}}
    if((CAN_1_ERR.ErrCode&0x0050)==0x0080){ui->label_CAN_1_ErrCode->setText("CAN控制器主动错误");}
        else{if(ui->label_CAN_1_ErrCode->text()=="CAN控制器主动错误"){ui->label_CAN_1_ErrCode->setText("---");}}
}


void CANset::on_pushButton_reset_clicked()
{
    dwRel_Rest=ResetCAN(nDeviceType, nDeviceInd, can_index);

    if (dwRel_Rest != STATUS_OK)
    { QMessageBox::information(this,"Rest",QString::fromLocal8Bit("NotOk")); }

}


void CANset::on_pushButton_off_clicked()
{
    canreadthread->terminate();
    cansdthread->terminate();
    ui->pushButton_StartCAN->setEnabled(true);
    ui->pushButton_off->setDisabled(true);
    ui->checkBox_can_0->setEnabled(true);
    ui->checkBox_can_1->setEnabled(true);
    ui->comboBox_Baud_rate->setEnabled(true);
    ui->comboBox_Baud_rate_1->setEnabled(true);
}





void CANset::on_comboBox_DevType_activated(int index)
{
    if(index==0)
    {
        nDeviceType=4;


    }
    else if(index==1)
    {
        nDeviceType=3;

    }

}


void CANset::on_comboBox_Baud_rate_activated(int index)
{
    if(index==0)
    {
        init_config.Timing0=0x01;
        init_config.Timing1= 0x1C;
    }
    else if(index==1)
    {
        init_config.Timing0=0x00;
        init_config.Timing1= 0x1C;
    }
}

void CANset::on_comboBox_Baud_rate_1_activated(int index)
{
    if(index==0)
    {
        can_1_init_config.Timing0=0x01;
        can_1_init_config.Timing1= 0x1C;
    }
    else if(index==1)
    {
        can_1_init_config.Timing0=0x00;
        can_1_init_config.Timing1= 0x1C                                                                                                                                                      ;
    }
}




void CANset::on_pushButton_datasave_clicked()
{
if(canfileName.isEmpty())
{
QMessageBox::information(this, "警告", "请先设置文件路径!");
    //qDebug()<<canfileName.isEmpty();
}
else
{
    if(ui->pushButton_datasave->text()=="开启记录")
    {


        QFile can_file(canfileName);
        if(!can_file.open(QIODevice::WriteOnly | QIODevice::Append))
        {
            QMessageBox::information(this, "警告", "文件被打开!");
        }
        else
        {
            QDateTime curDateTime=QDateTime::currentDateTime();
            QTextStream csvOutPut(&can_file);
            QString str ="CAN数据记录\n";
            QString str1 ="创建时间："+QString(curDateTime.toString("yyyy-MM-dd hh:mm:ss"))+"\n";
            QString str2 ="时间,CANID,时间间隔,Data0,Data1,Data2,Data3,Data4,Data5,Data6,Data7,通道\n";
            csvOutPut <<str.toUtf8();
            csvOutPut <<str1.toUtf8();
            csvOutPut <<str2.toUtf8();
            csvOutPut <<can_file.flush();
            can_file.close();
            can_save=1;
            ui->pushButton_datasave->setText("暂停记录");
            ui->pushButton_setfileroute->setEnabled(0);
        }


    }
    else
    {
        can_save=0;
        ui->pushButton_datasave->setText("开启记录");
        ui->pushButton_setfileroute->setEnabled(1);

    }

}

}





void CANset::on_pushButton_setfileroute_clicked()
{
    canfileName = QFileDialog::getSaveFileName(this,QStringLiteral("打开文件"),".",tr("csv files(*.csv)"));
}


void CANset::on_pushButton_clear_clicked()
{
    modelcan.clear();
    modelcan2.clear();
    modelcan.setHorizontalHeaderItem(0, new QStandardItem("系统时间") );
    modelcan.setHorizontalHeaderItem(1, new QStandardItem("设备时间") );
    modelcan.setHorizontalHeaderItem(2, new QStandardItem("CANID") );
    modelcan.setHorizontalHeaderItem(3, new QStandardItem("周期") );
    modelcan.setHorizontalHeaderItem(4, new QStandardItem("数据内容") );
    modelcan.setHorizontalHeaderItem(5, new QStandardItem("计数") );
    modelcan.setHorizontalHeaderItem(6, new QStandardItem("操作位") );

    modelcan2.setHorizontalHeaderItem(0, new QStandardItem("系统时间") );
    modelcan2.setHorizontalHeaderItem(1, new QStandardItem("设备时间") );
    modelcan2.setHorizontalHeaderItem(2, new QStandardItem("CANID") );
    modelcan2.setHorizontalHeaderItem(3, new QStandardItem("周期") );
    modelcan2.setHorizontalHeaderItem(4, new QStandardItem("数据内容") );
    modelcan2.setHorizontalHeaderItem(5, new QStandardItem("计数") );
    modelcan2.setHorizontalHeaderItem(6, new QStandardItem("操作位") );
}


void CANset::on_comboBox_nDeviceInd_activated(int index)
{
    if(index==0)
    {
            nDeviceInd=0;


    }
    else if(index==1)
    {
            nDeviceInd=1;

    }

    else if(index==2)
    {
            nDeviceInd=2;

    }


}

